package net.zjitc.controller;

import net.zjitc.pojo.Album;
import net.zjitc.pojo.Goods;
import net.zjitc.service.GoodsService;
import net.zjitc.utils.APIResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Value("${local.server.url}")
    private String localServerUrl;

    @Value("${file1.upload-dir}")
    private String uploadDir1;

    @GetMapping("/findAll")
    @ResponseBody
    public APIResponse<List<Goods>> findAll() {
        APIResponse<List<Goods>> apiResponse = new APIResponse<>();
        List<Goods> goodsList = goodsService.findAll();
        if (goodsList != null) {
            apiResponse.success("查询成功", goodsList);
            return apiResponse;
        }
        apiResponse.fail(1000, "查询失败", null);
        return apiResponse;
    }

    @GetMapping("/findOnePage")
    @ResponseBody
    public APIResponse<List<Goods>> findOnePage(@RequestParam("pagesize") int pagesize, @RequestParam("page") int page) {
        APIResponse<List<Goods>> apiResponse = new APIResponse<>();
        List<Goods> goodsList = goodsService.findOnePage(pagesize,page);
        if (goodsList != null) {
            apiResponse.success("查询成功", goodsList);
            return apiResponse;
        }
        apiResponse.fail(1000, "查询失败", null);
        return apiResponse;
    }

    @GetMapping("/findOne")
    @ResponseBody
    public APIResponse<Goods> findOne(@RequestBody @RequestParam("id") int id) {
        APIResponse<Goods> apiResponse = new APIResponse<>();
        Goods goods = goodsService.findOne(id);
        if (goods != null) {
            apiResponse.success("查询成功", goods);
            return apiResponse;
        }
        apiResponse.fail(1000, "查询失败", null);
        return apiResponse;
    }

    @PostMapping("/update")
    @ResponseBody
    public APIResponse<Goods> update(@RequestBody Goods goods) {
        APIResponse<Goods> apiResponse = new APIResponse<>();
        try {
            String picture = goods.getPicture();
            if(picture!=null&&picture.contains(localServerUrl)){
                goods.setPicture(picture.replace(localServerUrl,""));
            }
            if(picture!=null&&!picture.contains("static/")){
                goods.setPicture(uploadDir1+goods.getPicture());
            }
            goodsService.update(goods);
            goodsService.deleteAlbum(goods.getId());
            if(goods.getAlbumList() != null){
                for(int i = 0; i < goods.getAlbumList().size(); i++){
                    Album album = goods.getAlbumList().get(i);
                    if(album.getGoods_id()==null){
                        album.setGoods_id(goods.getId());
                    }
                    picture = album.getPicture();
                    if(picture!=null&&picture.contains(localServerUrl)){
                        album.setPicture(picture.replace(localServerUrl,""));
                    }
                    if(picture!=null&&!picture.contains("static/")){
                        album.setPicture(uploadDir1+picture);
                    }
                    goodsService.addAlbum(album);
                }
            }
            apiResponse.success("修改成功", goods);
            return apiResponse;
        } catch (Exception e) {
            e.printStackTrace();
            apiResponse.fail(1000, "修改失败", null);
            return apiResponse;
        }
    }
    @PostMapping("/add")
    @ResponseBody
    public APIResponse<Goods> add(@RequestBody Goods goods){
        APIResponse<Goods> apiResponse = new APIResponse<>();
        try{
            String picture = goods.getPicture();
            if(picture!=null&&picture.contains(localServerUrl)){
                goods.setPicture(picture.replace(localServerUrl,""));
            }
            if(picture!=null&&!picture.contains("static/")){
                goods.setPicture(uploadDir1+picture);
            }
            goodsService.add(goods);
            apiResponse.success("添加成功",goods);
            return apiResponse;
        }catch (Exception e){
            e.printStackTrace();
            apiResponse.fail(1000,"添加失败",null);
            return apiResponse;
        }
    }
    @GetMapping("/delete")
    @ResponseBody
    public APIResponse<Boolean> delete(@RequestBody @RequestParam("id")int id){
        APIResponse<Boolean> apiResponse = new APIResponse<>();
        try{
            goodsService.delete(id);
            apiResponse.success("删除成功",true);
            return apiResponse;
        }catch (Exception e){
            e.printStackTrace();
            apiResponse.fail(1000,"删除失败",null);
            return apiResponse;
        }
    }
}

